
#if 1

/*********************
 *      INCLUDES
 *********************/
#include "lv_port_indev.h"
#include "head_user.h"
#include "math.h"
/**********************
 *  STATIC PROTOTYPES
 **********************/
static void encoder_init(void);
static uint8_t encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static uint8_t touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);

static uint8_t button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static int8_t button_get_pressed_id(void);
static uint8_t button_is_pressed(uint8_t id);
/**********************
 *  STATIC VARIABLES
 **********************/
lv_indev_t * indev_encoder;
lv_indev_t * indev_button;

static int32_t encoder_diff;
static lv_indev_state_t encoder_state;

void lv_port_indev_init(void)
{
    //static lv_indev_drv_t indev_drv;

    /*------------------
     * Encoder
     * -----------------*/
    static lv_indev_drv_t Encoder_indev_drv;

    /*Register a encoder input device*/
    lv_indev_drv_init(&Encoder_indev_drv);
    Encoder_indev_drv.type = LV_INDEV_TYPE_ENCODER;
    Encoder_indev_drv.read_cb = encoder_read;
    indev_encoder = lv_indev_drv_register(&Encoder_indev_drv);

    /*------------------
     * Touchpad
     * -----------------*/
    static lv_indev_drv_t Touchpad_indev_drv;

    lv_indev_drv_init(&Touchpad_indev_drv);
    Touchpad_indev_drv.type = LV_INDEV_TYPE_POINTER;
    Touchpad_indev_drv.read_cb = touchpad_read;
    lv_indev_drv_register(&Touchpad_indev_drv);
    /*------------------
     * Button
     * -----------------*/
    static lv_indev_drv_t Button_indev_drv;

    /*Register a button input device*/
    lv_indev_drv_init(&Button_indev_drv);
    Button_indev_drv.type = LV_INDEV_TYPE_BUTTON;
    Button_indev_drv.read_cb = button_read;
    indev_button = lv_indev_drv_register(&Button_indev_drv);

    /*Assign buttons to points on the screen*/
    static const lv_point_t btn_points[4] = {
            {120, 160},     /*Button 0 -> x:10; y:10*/
            {40, 100},      /*Button 1 -> x:40; y:100*/
            {50, 160},
            {120, 200},
    };
    lv_indev_set_button_points(indev_button, btn_points);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
/* Will be called by the library to read the encoder */
static uint8_t encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    encoder_diff = (int32_t)(Encoder_Handle[enco_motor].diatance*10);
    encoder_state = LV_INDEV_STATE_REL;

    data->enc_diff = (int16_t)encoder_diff;
    data->state = encoder_state;

    /*Return `false` because we are not buffering and no more data to read*/
    return 0;
}
static uint8_t touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
        static uint16_t last_x = 0;
        static uint16_t last_y = 0;

        last_x = 120;
        last_y = (uint16_t)(Encoder_Handle[enco_motor].diatance*10);
        data->point.x = last_x;
        data->point.y = last_y;
        if(fabsf(INS_Handle.imuAngle->pitch)>20)data->state = LV_INDEV_STATE_PR;
        else data->state = LV_INDEV_STATE_REL;

    /*Return `false` because we are not buffering and no more data to read*/
    return 0;
}
/* Will be called by the library to read the button */
static uint8_t button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{

    static int8_t last_btn = -1;

    /*Get the pressed button's ID*/
    int8_t btn_act = button_get_pressed_id();

    if(btn_act >= 0) {
        data->state = LV_INDEV_STATE_PR;
        last_btn = btn_act;
    } else {
        data->state = LV_INDEV_STATE_REL;
    }

    /*Save the last pressed button's ID*/
    data->btn_id = last_btn;

    G_ioHandle.toggle(cpu1_led1);
    /*Return `false` because we are not buffering and no more data to read*/
    return 0;
}

/*Get ID  (0, 1, 2 ..) of the pressed button*/
static int8_t button_get_pressed_id(void)
{
    uint8_t i;

    /*Check to buttons see which is being pressed (assume there are 2 buttons)*/
    for(i = 0; i < 4; i++) {
        /*Return the pressed button's ID*/
        if(button_is_pressed(i)) {
            return i;
        }
    }

    /*No button pressed*/
    return -1;
}
/*Test if `id` button is pressed or not*/
static uint8_t button_is_pressed(uint8_t id)
{
    switch(id)
    {
        case 0:
        {
            if(*Key_Handle.ID == 9)return 1;
        }break;
        case 1:
        {
            if(*Key_Handle.ID == 10)return 1;
        }break;
        case 2:
        {
            if(*Key_Handle.ID == 13)return 1;
        }break;
        case 3:
        {
            if(*Key_Handle.ID == 14)return 1;
        }break;
        default:return 0;
    }
    return 0;
}
#else /* Enable this file at the top */

/* This dummy typedef exists purely to silence -Wpedantic. */
typedef int keep_pedantic_happy;
#endif
